package com.niit.spark.rdd.test

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Date:2025/5/9
 * Author：Ys
 * Description:
 */
object AggregateByKeyExercise2 {

  def main(args: Array[String]): Unit = {

    val sparkConf = new
        SparkConf().setMaster("local[*]").setAppName("AggregateByKeyExercise")
    val sc = new SparkContext(sparkConf)
    val rdd = sc.parallelize(Seq(("Apple", (2, 1.5)), ("Banana", (3, 2.0)),
      ("Apple", (1, 1.8)), ("Orange", (4, 2.5)), ("Banana", (2, 1.9))))

    val aggRdd: RDD[(String, (Int, Double, Int))] = rdd.aggregateByKey((0, 0.0, 0))(
      (acc, value) => (acc._1 + value._1, acc._2 + value._2, acc._3 + 1),
      (acc1, acc2) => (acc1._1 + acc2._1, acc1._2 + acc2._2, acc1._3 + acc2._3)
    )

    val resRdd: RDD[(String, (Int, Double))] = aggRdd.mapValues(x => (x._1, x._2 / x._3))

    resRdd.collect().foreach(println)

    sc.stop()




  }

}
